<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-1.11.0.min.js"></script>
</head>
<body>
    <form id="form">
        需要还款: <input readonly type="text" id="amount"><br>
        最少还款金额: <input readonly type="text" id="least"><br>
        账户还款: <select id="card2"></select><select id="c2amount"></select><input type="text" id="hkc2" placeholder="输入还款金额">
        <input type="button" id="btnc2" value="确定"><div id="c2"></div>
        银行卡还款: <select id="card1"></select><select id="c1amount"></select><input type="text" id="hkc1" placeholder="输入还款金额">
        <input type="button" id="btnc1" value="确定"><div id="c1"></div>
    </form>
    <table width="100%" id="shouye">
        <tr>
            <td align="center"><a href="/airong/index.html">首页</a></td>
            <!--    <td align="center"><a href="#"><input type="button" id="lc" value="理财"></a></td>-->
            <td align="center"><a href="" id="lc">理财</a></td>
            <td align="center"><a href="/airong/personalCenter.html">我的</a></td>

        </tr>
    </table>
</body>

    <script>
        //获取地址栏参数，name:参数名称
        function getUrlParms (name) {
            let url = window.location.href;//获取请求进来的完整url
            let tstr = url.substring(url.indexOf('?') + 1).split('&');//先截取url的?后面的参数部分，在根据&分割成参数数组
            let result = {};
            tstr.forEach((item) => {
                let res = item.split('=');//res为type,my-component1.vue。  res[0]为type，res[1为]my-component1.vue
                result[res[0]] = res[1];//构造成键值对形式 res[0]为键，res[1]为值 例：type: "my-component1.vue"
            });
            return result[name];//通过键取值
        };
        let uid = getUrlParms("uid");
        let cid = getUrlParms("cid");

        findHk()

        function findHk() {
            let url = "/airong/cc?action=findHk";
            let params = {uid: uid, cid: cid}
            $.get(url,params,function (data) {
                $("#amount").val(data.repayment);
                $("#least").val(data.leastAmount);
                $.each(data.c2,function (index,element) {
                    $("#card2").append("<option>"+ element.id +"</option>")
                });
                $.each(data.c1,function (index,element) {
                    $("#card1").append("<option>"+ element.id +"</option>")
                });
            },"json");

            $("#card2").change(function () {
                let cid = $("#card2").val();
                let url = "/airong/cc?action=finCard2ByID";
                let params = {cid: cid}
                $.get(url,params,function (data) {
                    $("#c2amount").html("")
                    $("#c2amount").append("<option>"+ data.balance +"</option>")
                },"json");
            });

            $("#card1").change(function () {
                let cid = $("#card1").val();
                let url = "/airong/cc?action=finCard1ByID";
                let params = {cid: cid}
                $.get(url,params,function (data) {
                    $("#c1amount").html("")
                    $("#c1amount").append("<option>"+ data.balance +"</option>")
                },"json");
            });

            $("#btnc2").click(function () {
                let c2input = $("#hkc2").val();
                let amount = $("#amount").val();
                let c2amount = $("#c2amount").val();
                let least = $("#least").val();
                if (Number.parseInt(c2input) > Number.parseInt(c2amount)){
                    $("#c2").html("");
                    $("#c1").html("");
                    $("#c2").append("<a style='color: red'>余额不足</a>")
                }else if (Number.parseInt(least) > Number.parseInt(c2input)){
                    $("#c2").html("");
                    $("#c1").html("");
                    $("#c2").append("<a style='color: red'>最少得还"+ least +"</a>")
                }else if (Number.parseInt(c2input) > Number.parseInt(amount)){
                    $("#amount").val(amount);
                }else {
                    $("#c2").html("");
                    $("#c1").html("");
                    let url = "/airong/cc?action=hkC2";
                    let params = {
                        ccID: cid,
                        num: c2input,
                        c2ID: $("#card2").val()
                    }
                    $.get(url,params,function (data) {
                        if (data === "Y"){
                            alert("还款成功")
                            window.location.href = "showCC.html?cid="+cid+"&uid="+uid;
                        }else {
                            alert("还款失败")
                            window.location.href = "showCC.html?cid="+cid+"&uid="+uid;
                        }
                    },"text")
                }
            });

            $("#btnc1").click(function () {
                let c1input = $("#hkc1").val();
                let amount = $("#amount").val();
                let c1amount = $("#c1amount").val();
                let least = $("#least").val();
                if (Number.parseInt(c1input) > Number.parseInt(c1amount)){
                    $("#c1").html("");
                    $("#c2").html("");
                    $("#c1").append("<a style='color: red'>余额不足</a>")
                }else if (Number.parseInt(least) > Number.parseInt(c1input)){
                    $("#c1").html("");
                    $("#c2").html("");
                    $("#c1").append("<a style='color: red'>最少得还"+ least +"</a>")
                }else if (Number.parseInt(c1input) > Number.parseInt(amount)){
                    $("#amount").val(amount);
                }else {
                    $("#c1").html("");
                    $("#c2").html("");
                    let url = "/airong/cc?action=hkC1";
                    let params = {
                        ccID: cid,
                        num: c1input,
                        c1ID: $("#card1").val()
                    }
                    $.get(url,params,function (data) {
                        if (data === "Y"){
                            alert("还款成功")
                            window.location.href = "showCC.html?cid="+cid+"&uid="+uid;
                        }else {
                            alert("还款失败")
                            window.location.href = "showCC.html?cid="+cid+"&uid="+uid;
                        }
                    },"text")
                }
            });
        }
        let urll = "/airong/cc?action=getSessionID";
        let paramsl = {}
        $.get(urll,paramsl,function (sessionID){
            document.getElementById('lc').href= "products.html?uid="+sessionID;
        },"text")

    </script>
<style>
    div {
        width: 100%;
        text-align: center;
    }
    #shouye{
        position: absolute;
        top: 600px;
        padding: 5px;
        margin: 4px;
        background-color: bisque;
        font-size: 20px;
        text-align: center;
        z-index: 1;
    }

</style>
</html>